/**
 * Created by zhangjinrui on 17/6/27.
 */

public class Solution129 {

    public static void main(String args[]){
        Solution129 s = new Solution129();
    }

    int sum;
    public int sumNumbers(TreeNode root) {
        sum = 0;
        if(root != null) {
            dfs(root, 0);
        }
        return sum;
    }

    public void dfs(TreeNode cur, int num){
        num = num * 10 + cur.val;
        if(cur.left == null && cur.right == null){
            sum += num;
            return;
        }
        if(cur.left != null){
            dfs(cur.left, num);
        }
        if(cur.right != null){
            dfs(cur.right, num);
        }
    }

}
